package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carrot.common.annotation.Log;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.PointInfoObjDataDto;
import com.carrot.common.core.domain.dto.SysPointDatasetDto;
import com.carrot.common.core.domain.entity.SysPointDataset;
import com.carrot.common.core.domain.entity.SysPointInfoObj;
import com.carrot.common.core.domain.vo.SysPointDatasetVo;
import com.carrot.common.core.domain.vo.SysPointInfoObjVo;
import com.carrot.common.enums.BusinessType;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.Page;
import com.carrot.common.utils.StringUtils;
import com.carrot.common.utils.poi.ExcelUtil;
import com.carrot.system.service.IPointDatasetService;
import com.carrot.system.service.IPointInfoObjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 信息对象地址
 */
@RestController
@RequestMapping("/system/point-dataset")
public class SysPointDatasetController extends BaseController {

    @Autowired
    private IPointDatasetService datasetService;
    @Autowired
    private IPointInfoObjService pointInfoObjService;

    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysPointDatasetDto dto) {

        Page<SysPointDataset, SysPointDatasetVo> page =
                new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<SysPointDataset> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(dto.getSearchContent())) {
            queryWrapper.like("dataset", dto.getSearchContent());
        }

        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);

        page = datasetService.findPage(page);

        return AjaxResult.success(page.getPageData());
    }

    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("id") Long id) {

        SysPointDataset dataset = datasetService.getById(id);
        return AjaxResult.success(dataset);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysPointDataset pointDataset) {

        datasetService.add(pointDataset);
        return AjaxResult.success();
    }


    @PostMapping("/update")
    public AjaxResult update(@RequestBody SysPointDataset pointDataset) {

        datasetService.update(pointDataset);
        return AjaxResult.success();
    }


    @GetMapping("/del")
    public AjaxResult del(@RequestParam("id") Long id) {

        datasetService.del(id);
        return AjaxResult.success();
    }


    @PostMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody SysPointDatasetDto dto) {
        if (CollUtil.isEmpty(dto.getIds())) {
            throw new ServiceException("参数校验失败");
        }
        datasetService.batchDel(dto.getIds());
        return AjaxResult.success();
    }

    /**
     * 点表导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "导入点表信息对象地址配置", businessType = BusinessType.IMPORT)
    @PostMapping("importPointInfoObj")
    public AjaxResult importPointInfoObj(MultipartFile file, Long datasetId) throws Exception {

        SysPointDataset dataset = datasetService.getById(datasetId);
        if (null == dataset){
            throw new ServiceException("数据集不存在");
        }

        ExcelUtil<PointInfoObjDataDto> util = new ExcelUtil<PointInfoObjDataDto>(PointInfoObjDataDto.class);

        List<PointInfoObjDataDto> dataList = util.importExcel(file.getInputStream());

        String returnMsg = pointInfoObjService.importData(datasetId, dataList);

        return AjaxResult.success(returnMsg);
    }


    @PostMapping("getDatasetPointList")
    public AjaxResult getDatasetPointList(@RequestBody SysPointDatasetDto dto) throws Exception {

        Page<SysPointInfoObj, SysPointInfoObjVo> page =
                new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<SysPointInfoObj> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("dataset_id", dto.getDatasetId());

        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);

        page = pointInfoObjService.findPage(page);

        return AjaxResult.success(page.getPageData());

    }

}
